/**
 * 
 */
package org.log4jeditor.xml.editor.validation;

import static org.junit.Assert.assertEquals;

import org.eclipse.core.resources.IResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.log4jconfig.xml.editor.validation.ContentValidator;
import org.log4jeditor.xml.editor.AbstractUITestCase;
import org.log4jeditor.xml.editor.DirtiesProjectFiles;

/**
 * @author Xi
 * 
 */
public class ParamContentValidatorTestCase extends AbstractUITestCase {

	private ContentValidator validator;

	@Before
	public void setup() throws Exception {
		setupEditorConfiguration("log4j-validation-param.xml");
		// remove all markers on document
		file.deleteMarkers(ContentValidator.DEFAULT_MARKER_ID, true,IResource.DEPTH_INFINITE) ;
		file.deleteMarkers(ContentValidator.DEPRECATED_ELEMENT_MARKER_ID, true,IResource.DEPTH_INFINITE) ;
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		
		validator = new ContentValidator();
		validator.connect(document);
	}

	@After
	public void tearDown() throws Exception {
		validator.disconnect(document);
		super.tearDownProject();
	}

	@Test
	public void testScanAndValidateValidParamName() throws Exception {
		int offset = getDocumentOffset(5, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	public void testScanAndValidateInvalidParamName() throws Exception {
		int offset = getDocumentOffset(7, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateEmptyParamName() throws Exception {
		int offset = getDocumentOffset(6, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateNewInvalidParamName() throws Exception {
		int offset = getDocumentOffset(5, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "Target".length(), "unknown");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateNewEmptyParamName() throws Exception {
		int offset = getDocumentOffset(17, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "Target".length(), "");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateFixInvalidParamName() throws Exception {
		int offset = getDocumentOffset(7, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "Unknown".length(), "Follow");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	public void testScanAndValidateKeepInvalidParamName() throws Exception {
		int offset = getDocumentOffset(7, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "Unknown".length(), "ReallyUnknown");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateKeepInvalidParamName2() throws Exception {
		int offset = getDocumentOffset(12, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "Unknown".length(), "");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	public void testScanAndValidateKeepValidParamName() throws Exception {
		int offset = getDocumentOffset(5, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "Target".length(), "Encoding");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateReplaceInvalidParamNameWithExistingOne() throws Exception {
		int offset = getDocumentOffset(12, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "Unknown".length(), "Target");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateReplaceValidParamNameWithExistingOne() throws Exception {
		int offset = getDocumentOffset(16, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "Target".length(), "Encoding");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateFixExistingParamNameWithValidOne() throws Exception {
		int offset = getDocumentOffset(12, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
		document.replace(offset, "Unknown".length(), "Encoding");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
	}

	@Test
	@DirtiesProjectFiles
	public void testScanAndValidateReplaceExistingParamNameWithInvalidOne() throws Exception {
		int offset = getDocumentOffset(16, 22, "param");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 0, getMarkers().length);
		document.replace(offset, "Target".length(), "unknown");
		validator.validate(getDirtyRegion(offset), null, null);
		assertEquals("Wrong number of markers:", 1, getMarkers().length);
	}
}
